package com.cjc.hotel.server.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjc.hotel.server.entity.TAdmin;
import com.cjc.hotel.server.entity.vo.AdminVo;
import com.cjc.hotel.server.handler.CommonException;
import com.cjc.hotel.server.result.Result;
import com.cjc.hotel.server.service.TAdminService;
import com.cjc.hotel.server.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 管理员控制器
 * @author chenjuncai
 */
@Controller
@RequestMapping("/admin/admin")
public class AdminController {
    @Autowired
    private TAdminService adminService;

    /**
     * 去列表页面
     * @return 结果
     */
    @GetMapping("")
    public String toList(){
        return "admin/admin";
    }
    /**
     * 去添加页面
     * @return 结果
     */
    @GetMapping("toAdd")
    public String toAdd(){
        return "admin/add";
    }
    /**
     * 修改页面
     * @param adminId
     * @param model
     * @return
     */
    @GetMapping("toEdit/{adminId}")
    public String toEdit(@PathVariable("adminId") Integer adminId, Model model)
    {
        TAdmin byId = adminService.getById(adminId);
        model.addAttribute("admin",byId);
        return "admin/edit";
    }
    /**
     * 保存管理
     * @param admin 管理
     * @return String
     */
    @ApiOperation(value = "保存管理员")
    @ResponseBody
    @PostMapping("/saveOrUpdate")
    public Result<String> saveOrUpdate(@Validated  TAdmin admin){
        // 判断用户名是否重复
        QueryWrapper<TAdmin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("username",admin.getUsername());
        int count = adminService.count(adminQueryWrapper);
        if(count>0&&admin.getAdminId()==null){
            throw new CommonException("用户名重复！");
        }
        // 插入数据库
        boolean b = adminService.saveOrUpdate(admin);
        return b? ResultUtil.success(""):ResultUtil.fail("操作失败");
    }

    /**
     *  删除管理
     * @param adminId 班别编号
     * @return String
     */
    @ApiOperation(value = "删除管理员")
    @ResponseBody
    @DeleteMapping("/remove/{adminId}")
    public Result<String> remove(@PathVariable("adminId") @ApiParam(name = "编号") String adminId){

        // 班别编号是否为空或空字符串
        if(null == adminId || "".equals(adminId)){
            throw new CommonException("管理员Id不存在！");
        }
        // 管理所属学生是否有
        QueryWrapper<TAdmin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("admin_id",adminId);
        int count = adminService.count(adminQueryWrapper);
        if(count==0){
            throw new CommonException("管理员不存在！");
        }
        // 删除
        boolean b = adminService.removeById(adminId);
        return b? ResultUtil.success(""):ResultUtil.fail("操作失败");
    }

    /**
     *  管理员列表
     * @param limit 页面记录数
     * @param page 当前页面
     * @return String
     */
    @ApiOperation(value = "管理员列表")
    @ResponseBody
    @GetMapping("/list")
    public Result<List<TAdmin>> list(@ApiParam(name = "page",value = "页面") @RequestParam Long page,
                                     @ApiParam(name = "limit",value ="页面记录数" ) @RequestParam Long limit,
                                     @ApiParam(name = "keyword",value ="关键字" ) @RequestParam(required = false) String keyword){
        QueryWrapper<TAdmin> tAdminQueryWrapper = null;
        if(keyword!=null){
            tAdminQueryWrapper = new QueryWrapper<>();
            tAdminQueryWrapper.like("username",keyword);
        }

        IPage<TAdmin> tAdminIPage = new Page<>(page,limit);
        return ResultUtil.defineSuccess(0,adminService.page(tAdminIPage,tAdminQueryWrapper).getRecords());
    }
}
